﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GoodsController : MonoBehaviour {
    public static GoodsController controller;
    private void Awake()
    {
        controller = this;
    }

    public List<Goods> goodsList = new List<Goods>();
    public List<Package> packageList = new List<Package>();
    public List<Package> CopypackageList = new List<Package>();
    public List<Goods> goodsobj = new List<Goods>();

    public UIPanel PackagePanel;
    public UIPanel CopyPackagePanel;

    public void DisableCollider(Goods goods,bool enable)
    {
        foreach (var item in goodsList)
        {
            if (!goods.Equals(item))
            {
                item.GetComponent<Collider>().enabled = enable;
            }
        }
    }

    public void ClearPackage()
    {
        for (int i = 0; i < CopypackageList.Count; i++)
        {
            if (CopypackageList[i].GetComponentInChildren<Goods>())
            {
                Goods go = CopypackageList[i].GetComponentInChildren<Goods>();
                goodsobj.Add(go);
            }
        }
        for (int j = 0; j < goodsobj.Count; j++)
        {
            for (int i = 0; i < packageList.Count; i++)
            {
                if (!packageList[i].GetComponentInChildren<Goods>())
                {
                    goodsobj[j].gameObject.transform.SetParent(packageList[i].transform);
                    goodsobj[j].gameObject.transform.localPosition = Vector3.zero;
                    break;
                }
            }
        }
        goodsobj.Clear();
    }

    Goods obj;
    /// <summary>
    /// 整理物品
    /// </summary>
    public void Arrangementequip()
    {
        for (int i = 0; i < packageList.Count; i++)
        {
            bool isHaving = packageList[i].GetComponentInChildren<Goods>();
            if (isHaving)
            {
                Goods go = packageList[i].GetComponentInChildren<Goods>();
                goodsobj.Add(go);
            }
            
        }
        for (int n = 0; n < goodsobj.Count - 1; n++)
        {
            for (int i = 0; i < goodsobj.Count - 1; i++)
            {
                if (goodsobj[i].i < goodsobj[i + 1].i)
                {
                    obj = goodsobj[i];
                    goodsobj[i] = goodsobj[i + 1];
                    goodsobj[i + 1] = obj;
                }
            }
        }
        for (int j = 0; j < goodsobj.Count; j++)
        {
            goodsobj[j].gameObject.transform.SetParent(packageList[j].transform);
            goodsobj[j].gameObject.transform.localPosition = Vector3.zero;
        }
        goodsobj.Clear();
    }

    /// <summary>
    /// 点击全部
    /// </summary>
    public void AllArrangement()
    {
        PackagePanel.gameObject.SetActive(true);
        CopyPackagePanel.gameObject.SetActive(false);
        ClearPackage();
    }

    /// <summary>
    /// 点击武器
    /// </summary>
    public void WarArrangement()
    {
        ClearPackage();
        CopyPackagePanel.gameObject.SetActive(true);
        PackagePanel.gameObject.SetActive(false);
        for (int i = 0; i < packageList.Count; i++)
        {
            bool isHaving = packageList[i].GetComponentInChildren<Goods>();
            if (isHaving && packageList[i].GetComponentInChildren<Goods>().goodsType == GoodsType.War)
            {
                Goods go = packageList[i].GetComponentInChildren<Goods>();
                goodsobj.Add(go);
            }
        }
        for (int n = 0; n < goodsobj.Count - 1; n++)
        {
            for (int i = 0; i < goodsobj.Count - 1; i++)
            {
                if (goodsobj[i].i < goodsobj[i + 1].i)
                {
                    obj = goodsobj[i];
                    goodsobj[i] = goodsobj[i + 1];
                    goodsobj[i + 1] = obj;
                }
            }
        }
        for (int j = 0; j < goodsobj.Count; j++)
        {
            goodsobj[j].gameObject.transform.SetParent(CopypackageList[j].transform);
            goodsobj[j].gameObject.transform.localPosition = Vector3.zero;
        }
        goodsobj.Clear();
    }

    /// <summary>
    /// 点击盾牌
    /// </summary>
    public void shieldArrangeament()
    {
        ClearPackage();
        CopyPackagePanel.gameObject.SetActive(true);
        PackagePanel.gameObject.SetActive(false);
        for (int i = 0; i < packageList.Count; i++)
        {
            bool isHaving = packageList[i].GetComponentInChildren<Goods>();
            if (isHaving && packageList[i].GetComponentInChildren<Goods>().goodsType == GoodsType.shield)
            {
                Goods go = packageList[i].GetComponentInChildren<Goods>();
                goodsobj.Add(go);
            }
        }
        for (int n = 0; n < goodsobj.Count - 1; n++)
        {
            for (int i = 0; i < goodsobj.Count - 1; i++)
            {
                if (goodsobj[i].i < goodsobj[i + 1].i)
                {
                    obj = goodsobj[i];
                    goodsobj[i] = goodsobj[i + 1];
                    goodsobj[i + 1] = obj;
                }
            }
        }
        for (int j = 0; j < goodsobj.Count; j++)
        {
            goodsobj[j].gameObject.transform.SetParent(CopypackageList[j].transform);
            goodsobj[j].gameObject.transform.localPosition = Vector3.zero;
        }
        goodsobj.Clear();
    }

    /// <summary>
    /// 点击鞋子
    /// </summary>
    public void ShoesArrangement()
    {
        ClearPackage();
        CopyPackagePanel.gameObject.SetActive(true);
        PackagePanel.gameObject.SetActive(false);
        for (int i = 0; i < packageList.Count; i++)
        {
            bool isHaving = packageList[i].GetComponentInChildren<Goods>();
            if (isHaving && packageList[i].GetComponentInChildren<Goods>().goodsType == GoodsType.Shoes)
            {
                Goods go = packageList[i].GetComponentInChildren<Goods>();
                goodsobj.Add(go);
            }
        }
        for (int n = 0; n < goodsobj.Count - 1; n++)
        {
            for (int i = 0; i < goodsobj.Count - 1; i++)
            {
                if (goodsobj[i].i < goodsobj[i + 1].i)
                {
                    obj = goodsobj[i];
                    goodsobj[i] = goodsobj[i + 1];
                    goodsobj[i + 1] = obj;
                }
            }
        }
        for (int j = 0; j < goodsobj.Count; j++)
        {
            goodsobj[j].gameObject.transform.SetParent(CopypackageList[j].transform);
            goodsobj[j].gameObject.transform.localPosition = Vector3.zero;
        }
        goodsobj.Clear();
    }

    /// <summary>
    /// 点击戒指
    /// </summary>
    public void RingArrangement()
    {
        ClearPackage();
        CopyPackagePanel.gameObject.SetActive(true);
        PackagePanel.gameObject.SetActive(false);
        for (int i = 0; i < packageList.Count; i++)
        {
            bool isHaving = packageList[i].GetComponentInChildren<Goods>();
            if (isHaving && packageList[i].GetComponentInChildren<Goods>().goodsType == GoodsType.Ring)
            {
                Goods go = packageList[i].GetComponentInChildren<Goods>();
                goodsobj.Add(go);
            }
        }
        for (int n = 0; n < goodsobj.Count - 1; n++)
        {
            for (int i = 0; i < goodsobj.Count - 1; i++)
            {
                if (goodsobj[i].i < goodsobj[i + 1].i)
                {
                    obj = goodsobj[i];
                    goodsobj[i] = goodsobj[i + 1];
                    goodsobj[i + 1] = obj;
                }
            }
        }
        for (int j = 0; j < goodsobj.Count; j++)
        {
            goodsobj[j].gameObject.transform.SetParent(CopypackageList[j].transform);
            goodsobj[j].gameObject.transform.localPosition = Vector3.zero;
        }
        goodsobj.Clear();
    }

    /// <summary>
    /// 删除物品
    /// </summary>
    public void DeleteGoods(Goods goods)
    {
        goodsList.Remove(goods);
        Destroy(goods.gameObject);
    }

    public UILabel num;
    // Use this for initialization
    void Start () {
        num.text = goodsList.Count.ToString();
    }
	
	// Update is called once per frame
	void Update () {
        num.text = goodsList.Count .ToString();
    }
}
